package com.dbappsecurity.jsbridge.api;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.dbappsecurity.jsbridge.contract.JsApiToggleContract;
import com.dbappsecurity.jsbridge.webview.CompletionHandler;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;

public class JsApiToggle {

  private JsApiToggleContract mContract;
  private Handler mHandler = new Handler(Looper.getMainLooper());
  private Gson gson = new Gson();

  public JsApiToggle(JsApiToggleContract contract) {
    mContract = contract;
  }

  @JavascriptInterface
  public void loading(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus loading: " + jsonObject.toString());
    mHandler.post(() -> {
      try {
        mContract.loading(jsonObject.getString("toggle"), jsonObject.getString("content"), handler);
      } catch (JSONException e) {
        try {
          mContract.loading(jsonObject.getString("toggle"), null, handler);
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
        e.printStackTrace();
      }
    });
  }

  @JavascriptInterface
  public void titleBar(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus loading: " + jsonObject.toString());
    mHandler.post(() -> {
      try {
        mContract.titleBar(jsonObject.getString("toggle"), handler);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    });
  }

  @JavascriptInterface
  public void appStart(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus appStart: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.appStart(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void homeClick(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus homeClick: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.homeClick(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void pushService(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus pushService: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.pushService(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void titleClick(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus titleClick: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.titleClick(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void keyboardSize(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus keyboardSize: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.keyboardSize(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void networkChange(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus networkChange: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.networkChange(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @JavascriptInterface
  public void progressBar(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiToggle", "Arirus progressBar: " + jsonObject.toString());
    try {
      final String tag = jsonObject.getString("toggle");
      if (null == tag) return;
      mHandler.post(() -> mContract.progressBar(tag, handler));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}
